package com.www.applyword.ui.my;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.Utils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2019/10/22 0022 15:59
 * version: 1.0
 * @author Administrator
 */
public class ResetPayActivity extends BaseActivity {
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.sb_ok)
    SuperButton sbOk;

    private VerificationCodeCountTimer verificationCodeCountTimer;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.reset_pay_activity);
        setTitle("设置支付密码");
    }

    @Override
    protected void loadData() {
        verificationCodeCountTimer = new VerificationCodeCountTimer(61100, 1000);
    }



    @OnClick({R.id.tv_code, R.id.sb_ok})
    public void onViewClicked(View view) {  //
        switch (view.getId()) {
            case R.id.tv_code:
                getCode();
                break;
            case R.id.sb_ok:
                if (!Utils.isPhoneNumber(etPhone.getText().toString())) {
                    Utils.showToastSafe("请输入正确的手机号码");
                    return;
                }
                if (TextUtils.isEmpty(etCode.getText().toString())) {
                    Utils.showToastSafe("请输入验证码");
                    return;
                }
                if (TextUtils.isEmpty(etPassword.getText().toString())) {
                    Utils.showToastSafe("请输入密码");
                    return;
                }
                setPassWord();
                break;
                default:
        }
    }

    private void setPassWord() {
        Map<String, Object> params = new HashMap<>();
        params.put("Mobile",etPhone.getText().toString());
        params.put("Pwd",etPassword.getText().toString());
        params.put("PhoneCode",etCode.getText().toString());
        HttpClient.get(ResetPayActivity.this, Constants.UPDATEPAYPWDBYMOBILE, params,new CallBack<String>() {
            @Override
            public void onSuccess(String result) {
                Utils.showToastSafe("设置支付密码成功");
               finish();
            }
        });
    }

    private void getCode() {
        if (!Utils.isPhoneNumber(etPhone.getText().toString())){
            Utils.showToastSafe("请输入正确的手机号码");
            return;
        }
        verificationCodeCountTimer.start();
        Map<String, Object> params = new HashMap<>();
        params.put("phone",etPhone.getText().toString());
        HttpClient.get(ResetPayActivity.this, Constants.GETSMSCODE, params,new CallBack<String>() {
            @Override
            public void onSuccess(String result) {
               // etCode.setText(result);
            }
        });
    }


    class VerificationCodeCountTimer extends CountDownTimer {

        private VerificationCodeCountTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            int time = (int) (Math.round((double) millisUntilFinished / 1000) - 1);
            tvCode.setTextColor(getResources().getColor(R.color.color_yellow2));
            tvCode.setText(time + "s");
        }

        @Override
        public void onFinish() {
            tvCode.setText("获取验证码");
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (verificationCodeCountTimer != null) {
            verificationCodeCountTimer.cancel();
        }
    }
}
